import axios from 'axios'
import { ElMessage } from 'element-plus'

// 创建Axios实例
const request = axios.create({
  baseURL: '/api', // 与代理配置对应
  timeout: 5000
})

// 请求拦截器
request.interceptors.request.use(
  config => {
    // 可在此添加token等请求头信息
    // config.headers.Authorization = `Bearer ${localStorage.getItem('token')}`
    return config
  },
  error => {
    ElMessage.error('请求出错，请稍后重试')
    return Promise.reject(error)
  }
)

// 响应拦截器
request.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    ElMessage.error(error.response?.data?.message || '服务器错误')
    return Promise.reject(error)
  }
)

export default request